473,466 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to clear checkboxes?

Hi,

I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:

<form name="myForm" method="post" action="MyPage.asp">
Jul 20 '05 #1
2 18621
In article <74**************************@posting.google.com >,
bi*********@hotmail.com (Bill S.) writes:
I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:
The fact that they all have the same name actually makes it easier. See below.

<form name="myForm" method="post" action="MyPage.asp">
.
. variable number of input fields
.
<input type="checkbox" value="2" name="memberOf" checked>
<input type="checkbox" value="1" name="memberOf" checked>
.
. repeats variable number of times
.

Is there another way to do this in JavaScript?

I can have the server side create client-side variables
that would tell me the form element number for the first
checkbox and the number of checkboxes if I have to, but
I would prefer something cleaner.


function clearCheckboxes(){
for (i=0;i<document.myForm.elements.length;i++)
{
if (document.myForm.elements[i].name == 'memberOf')
{
document.myForm.elements[i].checked = false;
}
}
}

<input type="button" value="Clear Member Boxes"
onclick="clearCheckboxes()" />
--
Randy
Jul 20 '05 #2
Bill S. wrote:
I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:
[...]

Is there another way to do this in JavaScript?


Another way? You have not even shown your way.

Anyway :-) elements of the same name create a collection.
You can iterate this collection:

var c = document.forms[...].elements["..."];
if (c)
{
for (var i = 0; i < c.length; i++)
{
c[i].checked = false;
}
}
HTH

PointedEars
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
1
by: bestafor | last post by:
I have created, in Borland c++ builder 5.0, a dialog box With 24 CheckBoxes, named CheckBox1 through CheckBox24. My question is how may I clear all Twenty-four CheckBoxes by pressing a “Clear”...
5
by: Matt | last post by:
I want to write a generic clearForm function to clear the form when the user click CLEAR button. Here's my attempts, because I want to take care all html controls. I think I need to test if the...
2
by: Tina | last post by:
Is there anyway to clear a checkbox list (uncheck all items) on the client side without causing a trip to the server? If so, could someone show me how? Thanks, T
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
3
by: Robert Kilroy | last post by:
Greetings, I've been working on this for a few hours now. It seems to be a pretty simple task but I keep running into " has no properties". I have a select box defined as follows: <SELECT...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
2
by: mcmahon | last post by:
Hi, I have a pile of checkboxes on one particular form and would like to clear them all at the form load rather that saying chk1.Checked = False for all of them. Here is the code I was trying...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.